package cn.com.huzo.service.trade.impl;

import cn.com.huzo.model.entities.trade.DeliveryCommandSheetHeaderEntity;
import huzofw.model.entities.paginator.Paginator;
import huzofw.service.impl.GenericManagerImpl;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.annotation.Resource;
import org.springframework.stereotype.Service;
import cn.com.huzo.repository.trade.DeliveryCommandSheetHeaderRepository;
import cn.com.huzo.repository.trade.DeliveryCommandSheetLinesMoRepository;
import cn.com.huzo.repository.trade.DeliveryCommandSheetLinesRepository;
import cn.com.huzo.service.trade.DeliveryCommandSheetHeaderManager;
import org.apache.commons.collections4.MapUtils;
import org.springframework.transaction.annotation.Transactional;

/**
 * 发货发令单管理服务对象类实现。
 *
 * @author 吴旭
 */
@Service("DeliveryCommandSheetHeaderManager")
public class DeliveryCommandSheetHeaderManagerImpl extends GenericManagerImpl<DeliveryCommandSheetHeaderEntity, Long>
        implements DeliveryCommandSheetHeaderManager {

    @Resource(name = "DeliveryCommandSheetHeaderRepository")
    private DeliveryCommandSheetHeaderRepository _dao_header;

    @Resource(name = "DeliveryCommandSheetLinesRepository")
    private DeliveryCommandSheetLinesRepository _dao_lines;

    @Resource(name = "DeliveryCommandSheetLinesMoRepository")
    private DeliveryCommandSheetLinesMoRepository _dao_lines_mo;

    /**
     * {@inheritDoc}
     *
     * @throws java.lang.Exception
     */
    @Transactional
    @Override
    public Map<String, Object> fetchByHeaderKey(Long headerId) throws Exception {
        Map<String, Object> rtnValue = new HashMap();

        rtnValue.put("HeaderEntity", _dao_header.fetchByKey(headerId));
        rtnValue.put("LinesList", _dao_lines.fetchByHeaderId(headerId));
        rtnValue.put("LinesMoList", _dao_lines_mo.fetchByHeaderId(headerId));
        return rtnValue;
    }

    /**
     * {@inheritDoc}
     *
     * @throws java.lang.Exception
     */
    @Transactional
    @Override
    public String fetchNewBillNo() throws Exception {
        return _dao_header.fetchNewBillNo();
    }

    /**
     * {@inheritDoc}
     *
     * @throws java.lang.Exception
     */
    @Transactional
    @Override
    public String fetchStyleNoByHeaderKey(Long headerId) throws Exception {
        return _dao_header.fetchStyleNoByHeaderKey(headerId);
    }

    /**
     * {@inheritDoc}
     *
     * @throws java.lang.Exception
     */
    @Transactional
    @Override
    public Map<String, Object> fetchForPage(Map<String, Object> criterion) throws Exception {
        Map<String, Object> rtnValue = new HashMap();

        Long offset = (long) (MapUtils.getInteger(criterion, "Limit") * (MapUtils.getInteger(criterion, "PageIndex") - 1));
        long total = _dao_header.fetchForCount(criterion);

        if (total > 0) {
            if (offset >= total) {
                offset = (total - MapUtils.getInteger(criterion, "Limit")) < 0 ? 0 : (total - MapUtils.getInteger(criterion, "Limit"));
            }
            criterion.put("Offset", offset);
            List<DeliveryCommandSheetHeaderEntity> itemList = _dao_header.fetchForPage(criterion);
            Paginator<DeliveryCommandSheetHeaderEntity> paginator = new Paginator<>(itemList, offset, MapUtils.getInteger(criterion, "Limit"), total);

            rtnValue.put("TotalRecordCount", paginator.getTotal());
            rtnValue.put("TotalPages", paginator.getTotalPages());
            rtnValue.put("PageIndex", paginator.getPageIndex());
            rtnValue.put("DataList", paginator.getItemList());
            rtnValue.put("Success", true);
        } else {
            rtnValue.put("Success", false);
        }

        return rtnValue;
    }

    /**
     * {@inheritDoc}
     *
     */
    @Transactional
    @Override
    public Long insert(DeliveryCommandSheetHeaderEntity instance) throws Exception {
        Long key = _dao_header.insert(instance);
        if (key > 0) {
            // 审计日志
            writeAuditLog(instance.getUpdatedBy(), "贸易信息→发货发令单", "单据号：" + instance.getBillNo(), "增加");
        }

        return key;
    }

    /**
     * {@inheritDoc}
     *
     * @throws java.lang.Exception
     */
    @Transactional
    @Override
    public Integer update(DeliveryCommandSheetHeaderEntity instance) throws Exception {
        Integer rowsAffected = _dao_header.update(instance);
        if (rowsAffected > 0) {
            // 审计日志
            writeAuditLog(instance.getUpdatedBy(), "贸易信息→发货发令单", "单据号：" + instance.getBillNo(), "更新");
        }

        return rowsAffected;
    }

    /**
     * {@inheritDoc}
     *
     * @throws java.lang.Exception
     */
    @Transactional
    @Override
    public Map<String, Object> delete(Map<String, Object> criterion) throws Exception {
        // 删除
        Integer rowsAffected = _dao_header.delete(MapUtils.getLong(criterion, "HeaderId"));
        if (rowsAffected > 0) {
            // 审计日志
            writeAuditLog(MapUtils.getLong(criterion, "DeletedBy"), "贸易信息→发货发令单", "单据号：" + MapUtils.getString(criterion, "BillNo"), "删除");
        }
        return fetchForPage(criterion);
    }
}
